<template>
    <div class="prepare-add i_maxW">
        <div class="prepare-crumbs">
            <router-link to="/prepare">备课<i>></i></router-link>
            <span @click="goBack">{{$route.query.name}} </span>
            <span><i>></i>编辑</span>
        </div>
        <div class="prepare-add-cont">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="备课类型" prop="type">
                    <el-radio-group v-model="ruleForm.type">
                        <el-radio :label="0" v-if="ruleForm.type == 0">个人备课</el-radio>
                        <el-radio :label="1" v-if="ruleForm.type == 1">集体备课</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="备课名称" prop="name">
                    <el-input v-model="ruleForm.name" maxlength="30"></el-input>
                </el-form-item>

                <el-form-item label="备课目标" prop="target" class="prepare-target">
                    <el-input type="textarea" :rows="4" resize="none" v-model="ruleForm.target" maxlength="200" placeholder="请输入备课目标"></el-input>
                    <span><i>{{ruleForm.target.length}}</i>/200</span>
                </el-form-item>

                <el-form-item label="截止日期" prop="date" class="prepare-date" v-if="ruleForm.type == 1">
                   <!-- <el-date-picker type="date"  placeholder="选择日期" value-format="yyyy-MM-dd"  v-model="ruleForm.date" :picker-options="pickerOptionsStart"></el-date-picker> -->
                     <el-date-picker
                      v-model="ruleForm.date"
                       type="datetime"  placeholder="选择日期"
                      value-format="yyyy-MM-dd HH:mm" :picker-options="pickerOptionsStart">
                   </el-date-picker>
                </el-form-item>

                <el-form-item  label-width="46px" class="prepare-add-select">
                    <selection ref="selection" required></selection>
                        <!-- <el-form-item label="学段" prop="study_section_id">
                            <el-select v-model="ruleForm.study_section_id" placeholder="请选择学段"  @change="relateChange($event,'study_section')">
                                <template  v-for="(item ,inx) of orgLevel2">
                                    <el-option :label="item.name" :value="item.id"  :key="inx"></el-option>
                                </template>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="年级" prop="grade_id">
                            <el-select v-model="ruleForm.grade_id" placeholder="请选择年级"  @change="relateChange($event,'grade')">
                                <template  v-for="(item ,inx) of orgLevel3">
                                    <el-option :label="item.name" :value="item.id"  :key="inx"></el-option>
                                </template>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="科目" prop="subject_id">
                            <el-select v-model="ruleForm.subject_id" placeholder="请选择活动区域" @change="relateChange($event,'subject')">
                                 <template  v-for="(item ,inx) of orgSubject">
                                    <el-option :label="item.name" :value="item.id"  :key="inx"></el-option>
                                </template>
                            </el-select>
                        </el-form-item> -->
                </el-form-item>

                <template v-if="ruleForm.type == 1">
                    <el-form-item label="邀请老师" prop="teachArr" class="prepare-invitation">
                       <el-button type="primary" @click="inviteTeacher">选择</el-button>
                       <div>
                           <el-tag v-for="(tag, index) in teachlistID" :key="tag.name" closable @close="handleClose(index)">
                             {{tag.name}}
                           </el-tag>
                       </div>
                        <el-checkbox-group v-model="ruleForm.teachArr">
                            <el-checkbox v-for="(item, index) of teachlistID" :label="item.id" name="type" :key="index"></el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>
                </template>

                <template>
                    <el-form-item label="上传教案" class="prepare-add-upBtn">
                        <el-button type="primary" plain @click="videoShow()">关联视频</el-button>
                        <el-button type="primary" plain @click="testShow()">关联试卷</el-button>
                        <el-button type="primary" plain @click="resourceShow()">关联资源</el-button>
                        <el-button type="primary" plain >
                            <uploader 
                            :options="uploadOptions" 
                            :autoStart="true" 
                            ref="uploaderRes" 
                            :file-status-text="statusText" 
                            @file-added="fileAddRes" 
                            @file-success="fileSuccessRes" 
                            @file-error="fileFailRes" 
                            class="uploader-add-album">
                                <uploader-unsupport></uploader-unsupport>
                                <uploader-btn :directory="false" :single="true">本地上传</uploader-btn>
                                <!-- <uploader-list></uploader-list> -->
                            </uploader>
                        </el-button>
                    </el-form-item>

                    <div class="prepare-relation">
                        <h3>视频</h3>
                        <ul>
                            <li class="prepare-relation-list clear" v-for="(item, index) of ruleForm.videoArr" :key="index">
                                <span class="fl">{{item.name}}</span>
                                <i class="el-icon-close fr" @click="removeVideo(index)"></i>
                            </li>
                        </ul>
                    </div>

                    <div class="prepare-relation">
                        <h3>试卷</h3>
                        <ul>
                            <li class="prepare-relation-list clear" v-for="(item, index) of ruleForm.testArr" :key="index">
                                <span class="fl">{{item.name}}</span>
                                <i class="el-icon-close fr" @click="removeTest(index)"></i>
                            </li>
                        </ul>
                    </div>

                    <div class="prepare-relation">
                        <h3>资源</h3>
                        <ul>
                            <li class="prepare-relation-list clear" v-for="(item, index) of ruleForm.resourceArr" :key="index">
                                <span class="fl">{{item.name}}</span>
                                <i class="el-icon-close fr" @click="removeResource(index)"></i>
                            </li>
                        </ul>
                    </div>
                </template>

                <el-form-item class="prepare-submit buttonCSS" label-width="0">
                    <el-button @click="resetForm('ruleForm')" plain>取消</el-button>
                    <el-button type="primary" v-preventReClick="1000" @click="submitForm('ruleForm')">保存</el-button>
                </el-form-item>
            </el-form>
        </div>

        <!-- 选择教师 -->
        <el-dialog
          title="选择教师"
          :visible.sync="teachVisible"
          width="800px"
          custom-class="relation-dialog"
          :close-on-click-modal="false">
            <div class="relation-cont">
                <div class="relation-op" v-if="0">
                    <el-input placeholder="请输入视频名称"
                         suffix-icon="el-icon-search"
                         v-model="teachKeyword">
                    </el-input>
                </div>
                <!--  -->
                <div class="relation-batle" v-loading="loading4"  element-loading-text="拼命加载中">
                    <template>
                      <el-table
                        ref="multipleTeach"
                        :data="teachList"
                        :border="true"
                        tooltip-effect="dark"
                        style="width: 100%"
                        @selection-change="teachSelectionChange">
                        <el-table-column
                          type="selection"
                          width="40">
                        </el-table-column>
                        <el-table-column
                          prop="number"
                          label="登录账户"
                          show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                          prop="name"
                          label="用户名"
                          width="120"
                          show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                          prop="sub_name"
                          label="科目"
                          show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                          prop="org_name"
                          label="组织架构"
                          show-overflow-tooltip>
                        </el-table-column>
                      </el-table>
                    </template>
                    <el-pagination
                         @current-change="teachChange"
                         :current-page.sync="page_index4"
                         :page-size="page_size4"
                         layout="total, prev, pager, next"
                         :total="total_sum4">
                    </el-pagination>
                </div>
                <div class="relation-btn buttonCSS">
                    <el-button type="primary" @click="teachVisible = false" plain>取消</el-button>
                    <el-button type="primary" @click="teachSbulmit()">确定</el-button>
                    <!-- <el-button type="primary" @click="toggleSelection(teachList2)">确定2</el-button> -->
                </div>
            </div>
        </el-dialog>
        <!-- 选择视频 -->
        <el-dialog title="选择视频" :visible.sync="videoVisible" width="800px" custom-class="relation-dialog" :close-on-click-modal="false">
            <div class="relation-cont">
                <div class="relation-op">
                    <el-select v-model="videoType" placeholder="请选择" @change="changeVideoList">
                        <el-option label="全部" :value="-1"> </el-option>
                        <el-option label="公开课" :value="0"> </el-option>
                    </el-select>
                    <el-input placeholder="请输入视频名称" suffix-icon="el-icon-search" @change="changeVideoList" v-model="videoKeyword">
                    </el-input>
                </div>
                <!--  -->
                <div class="relation-batle" v-loading="loading">
                    <template>
                        <el-table ref="multipleTable" :data="videoList" :border="true" tooltip-effect="dark" style="width: 100%" @selection-change="videoSelectionChange">
                            <el-table-column type="selection" width="40">
                            </el-table-column>
                            <el-table-column prop="video_name" label="视频名称" show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column prop="category" label="视频分类" show-overflow-tooltip>
                            </el-table-column>
                          <!--   <el-table-column prop="profile" label="视频信息" show-overflow-tooltip>
                            </el-table-column> -->
                            <el-table-column prop="create_time" label="上传时间" show-overflow-tooltip>
                            </el-table-column>
                        </el-table>
                    </template>
                    <el-pagination @current-change="videoChange" :current-page.sync="page_index" :page-size="page_size" layout="total, prev, pager, next" :total="total_sum">
                    </el-pagination>
                </div>
                <div class="relation-btn buttonCSS">
                    <el-button type="primary" @click="videoVisible = false" plain>取消</el-button>
                    <el-button type="primary" @click="vodeoSbulmit()">确定</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 选择试卷 -->
        <el-dialog title="选择试卷" :visible.sync="testVisible" width="800px" custom-class="relation-dialog" :close-on-click-modal="false">
            <div class="relation-cont">
                <div class="relation-op">
                    <el-select v-model="testType" placeholder="请选择" @change="changegTestList" v-if='0'>
                        <el-option label="全部" :value="-1"> </el-option>
                        <el-option label="公开" :value="1"> </el-option>
                        <el-option label="私人" :value="0"> </el-option>
                    </el-select>
                    <el-input placeholder="请输入试卷名称" suffix-icon="el-icon-search" @change="changegTestList" v-model="testKeyword">
                    </el-input>
                </div>
                <!--  -->
                <div class="relation-batle" v-loading="loading2" element-loading-text="拼命加载中">
                    <template>
                        <el-table ref="multipleTable" :data="testList" :border="true" tooltip-effect="dark" style="width: 100%" @selection-change="testSelectionChange">
                            <el-table-column type="selection" width="40">
                            </el-table-column>
                            <el-table-column prop="name" label="试卷名称" show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column prop="grade" label="试卷分类" show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column prop="create_time" label="组卷时间" show-overflow-tooltip>
                            </el-table-column>
                        </el-table>
                    </template>
                    <el-pagination @current-change="testChange" :current-page.sync="page_index2" :page-size="page_size2" layout="total, prev, pager, next" :total="total_sum2">
                    </el-pagination>
                </div>
                <div class="relation-btn buttonCSS">
                    <el-button type="primary" @click="testVisible = false" plain>取消</el-button>
                    <el-button type="primary" @click="testSbulmit()">确定</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 选择资源 -->
        <el-dialog title="选择资源" :visible.sync="resourceVisible" width="800px" custom-class="relation-dialog" :close-on-click-modal="false">
            <div class="relation-cont">
                <div class="relation-op">
                     <el-select v-model="resourceType" placeholder="请选择" @change="changeResourceList" v-if='0'>
                        <el-option label="全部" :value="-1"> </el-option>
                        <el-option label="公开资源" :value="1"> </el-option>
                        <el-option label="私人资源" :value="0"> </el-option>
                    </el-select>
                    <el-input placeholder="请输入资源名称" suffix-icon="el-icon-search" @change="changeResourceList" v-model="resourceKeyword">
                    </el-input>
                </div>
                <!--  -->
                <div class="relation-batle" v-loading="loading3" element-loading-text="拼命加载中">
                    <template>
                        <el-table ref="multipleTable" :data="resourceList" :border="true" tooltip-effect="dark" style="width: 100%" @selection-change="resourceSelectionChange">
                            <el-table-column type="selection" width="40">
                            </el-table-column>
                            <el-table-column prop="name" label="资源名称" show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column prop="category" label="资源分类"  show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column prop="create_time" label="创建时间" show-overflow-tooltip>
                            </el-table-column>
                        </el-table>
                    </template>
                    <el-pagination @current-change="resourceChange" :current-page.sync="page_index3" :page-size="page_size3" layout="total, prev, pager, next" :total="total_sum3">
                    </el-pagination>
                </div>
                <div class="relation-btn buttonCSS">
                    <el-button type="primary" @click="resourceVisible = false" plain>取消</el-button>
                    <el-button type="primary" @click="resourceSbulmit()">确定</el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>
<script>
let videoList = [{
    date: '2016-05-02',
    name: '王小虎',
    address: '上海市普陀区金沙江路 1518 弄'
}]
import selection  from  './prepare-add-select';
export default {
    components: {
        selection
    },       
    data() {
        return {
            pickerOptionsStart: {
                disabledDate: (time) => {
                    return time.getTime() < parseInt(Date.now() - 8.64e7);
                }
            },
            ruleForm: {
                type: this.$route.query.type,
                name: '',
                target: '',
                date: '',
                study_section_id: '',
                grade_id: '',
                subject_id: '',
                teachArr: [],
                videoArr: [],
                testArr: [],
                resourceArr: [],
                teachList: []
            },
            rules: {
                type: [
                    { required: true, message: '请选择活动资源', trigger: 'change' }
                ],
                name: [
                    { required: true, message: '请输入备课名称', trigger: 'blur' },
                ],
                target: [
                    { required: true, message: '请输入备课目标', trigger: 'blur' }
                ],
                date: [
                    {  required: true, message: '请选择日期', trigger: 'change' }
                ],

                teachArr: [
                    { required: true, message: '请选择邀请老师', trigger: 'change' }
                ]
            },

            teachVisible: false,
            teachKeyword: '',
            teachlection: [],
            teachList: [],
            page_index4: 1,
            page_size4: this.$g.page_size,
            total_sum4: 0,
            loading4: true,

            videoVisible: false,
            videoType: -1,
            videoKeyword: '',
            videoSelection: [],
            videoList: [],
            page_index: 1,
            page_size: this.$g.page_size,
            total_sum: 0,
            loading: true,

            testVisible: false,
            testType: 0,
            testKeyword: '',
            testSelection: [],
            testList: [],
            page_index2: 1,
            page_size2: this.$g.page_size,
            total_sum2: 0,
            loading2: true,

            resourceVisible: false,
            resourceType: 0,
            resourceKeyword: '',
            resourcelection: [],
            resourceList: [],
            page_index3: 1,
            page_size3: this.$g.page_size,
            total_sum3: 0,
            loading3: true,



            statusText: {
                success: '上传成功',
                error: '上传出错',
                uploading: '正在上传...',
                paused: '暂停',
                waiting: '等待上传'
            },
            selectFileOrNot: false, //是否选中文件
            uploadInfo: {
                file_name: '',
                file_path: '',
                file_size: 0
            },
            attrs: {
                accept: ['image/*']
            },

            uploadOptions: {
                target: this.BASE_URL + '/api/resource/res_upload',
                testChunks: false,
                headers: { Authorization: `Bearer ` + this.$g.access_token },
            },
            selectFileShow: true, //是否显示选择文件按钮
            uploadRetryTimes: 100, //文件最大重传次数
            previewImgURL: '',
            fileNumber: 0,
            uploadNumber: 0,
            loading4: false,
            teachlistID: [],
            sunNmber: 0,
            teachArr: []
        };
    },
    created(){
        this.getTeacher()
        this.getPrepare_Date()

    },
    mounted(){
        this.$nextTick(function () {
           
        })
    },
    methods: {
        goBack(){
            this.$router.back(-1)
        },
        // //学段选择
        // relateChange(newVal, type) {

        //     switch (type) {
        //         case 'study_section':

        //             this.ruleForm.grade_id = null;
        //             this.ruleForm.subject_id = null;

        //             this.orgLevel3 = [];
        //             this.orgSubject = [];

        //             this.orgLevelParam.pid = newVal;
        //             this.getOrgLevel3();
        //             break;
        //         case 'grade':
        //             this.ruleForm.subject_id = null;
        //             this.orgSubject = [];

        //             this.subjectParam.org_id = newVal;
        //             this.getSubject();
        //             break;
        //         default:
        //             break;
        //     }
        // },
        // //获取学段
        // getOrgLevel2() {
        //     let success = (res => {
        //         this.orgLevel2 = res.data;
               
        //     });

        //     this.$ajax({
        //         url: 'api/org/org',
        //         method: 'get',
        //         params: this.orgLevelParam,
        //         func: { success: success },
        //     })
        // },
        // //获取年级
        // getOrgLevel3() {

        //     this.orgLevelParam.level = 3;
        //     this.orgLevelParam.pid = this.ruleForm.study_section_id;
        //     let success = (res => {
        //         this.orgLevel3 = res.data;
        //         // this.getSubject()
        //     })

        //     this.$ajax({
        //         url: 'api/org/org',
        //         method: 'get',
        //         params: this.orgLevelParam,
        //         func: { success: success }
        //     })
        // },
        // //获取科目
        // getSubject() {

        //     this.subjectParam.org_id = this.ruleForm.grade_id;

        //     let success = (res => {
        //         this.orgSubject = res.data
        //     });

        //     this.$ajax({
        //         url: 'api/cate/subject',
        //         method: 'get',
        //         params: this.subjectParam,
        //         func: { success: success }
        //     })
        // },
        //选择老师--弹窗
        inviteTeacher() {
            this.teachVisible = true;
            this.teachlection = []
            this.getTeacher()
        },
        //删除选择的老师
        handleClose(index) {
            this.teachlistID.splice(index, 1);
            this.ruleForm.teachArr.splice(index, 1)
        },
        teachChange(val) {
            this.page_index = val;
            this.getTeacher()
        },
        teachSelectionChange(val) {
            this.teachlection = val;
        },

        // 视频
        videoShow() {
            this.videoVisible = true;
            this.getVideoList()
        },
        changeVideoList() {
            this.page_index = 1
            this.getVideoList()
        },
        getVideoList() {
            let success = (res => {
                this.videoList = res.data.data;
                this.total_sum = parseInt(res.data.data_sum)
                this.loading = false;
            })

            this.$ajax({
                url: 'api/video/video_list',
                method: 'get',
                params: {
                  page_index: this.page_index,
                  page_size: this.page_size,
                  status: 1,
                  authority: 1,
                  video_name: this.videoKeyword,
                  type: this.videoType,
                  check_app_code: sessionStorage.getItem('CheckSchoolAppCode'),
                                    // create_id: this.$g.user_id,
                                    // other_authority: 1,

                },
                func: { success: success }
            })
        },
        removeVideo(index) {
            this.ruleForm.videoArr.splice(index, 1)
        },
        // 选择视频
        vodeoSbulmit() {
            if (this.videoSelection.length === 0) {
                this.$message.error('请勾选');
                return
            }

            for (let i = 0; i < this.videoSelection.length; i++) {
                this.ruleForm.videoArr.push({
                    name: this.videoSelection[i].video_name,
                    id: this.videoSelection[i].id,
                    origin: 0
                })
            }
            /*去重 */
            let hash = {};
            let arr = this.ruleForm.videoArr.reduceRight((item, next) => {
                hash[next.id] ? '' : hash[next.id] = true && item.push(next);
                return item
            }, [])

            this.ruleForm.videoArr = arr

            this.videoVisible = false;
        },
        videoChange(val) {
            this.page_index = val;
            this.getVideoList()
        },
        videoSelectionChange(val) {
            this.videoSelection = val;
        },
        // 试卷
        testShow() {
            this.testVisible = true
            this.getTestList()
        },
        changegTestList(){
            this.page_index2 = 1
            this.getTestList()
        },
        getTestList() {
            let success = (res => {
                this.testList = res.data.data;
                this.total_sum2 = parseInt(res.data.data_sum)
                this.loading2 = false
            });

            this.$ajax({
                url: 'api/test/test',
                method: 'get',
                params: {
                     keyword: this.testKeyword,
                    page_index: this.page_index2,
                    page_size: this.page_size2,
                    status: 1,
                    authority: 1,
                    // status: '1,2,3',
                    // user_id: this.$g.user_id,
                    // other_authority:this.testType,
                    // authority: this.testType
                    check_app_code:sessionStorage.getItem('CheckSchoolAppCode')
                },
                func: { success: success }
            })
        },
        testSbulmit() {
            if (this.testSelection.length === 0) {
                this.$message.error('请勾选');
                return
            }

            for (let i = 0; i < this.testSelection.length; i++) {
                this.ruleForm.testArr.push({
                    name: this.testSelection[i].name,
                    id: this.testSelection[i].id,
                    origin: 1
                })
            }
            /*去重 */
            let hash = {};
            let arr = this.ruleForm.testArr.reduceRight((item, next) => {
                hash[next.id] ? '' : hash[next.id] = true && item.push(next);
                return item
            }, [])

            this.ruleForm.testArr = arr

            this.testVisible = false;
        },
        removeTest(index) {
            this.ruleForm.testArr.splice(index, 1)
        },
        testChange(val) {
            this.page_index2 = val;
            this.getTestList()
        },
        testSelectionChange(val) {
            this.testSelection = val;
        },
        // 资源
        resourceShow() {
            this.resourceVisible = true;
            this.getResourceList()
        },
        removeTest(index) {
            this.ruleForm.resourceArr.splice(index, 1)
        },
        changeResourceList(){
            this.page_index3 = 1
            this.getResourceList()
        },
        getResourceList() {
            let success = (res => {
                this.resourceList = res.data.data;
                this.total_sum3 = parseInt(res.data.data_sum)
                this.loading3 = false;
            });

           let params = '';
            // 全部 -1, 公开 1, 私人 0

            if (this.resourceType == -1) {
                params = {

                    resource_name: this.resourceKeyword,
                    page: this.page_index3,
                    size: this.page_size3,
                    data_type: 2,
                    // status: 1,
                    // authonity: 0,
                    // create_id: this.$g.user_id,
                    // other_authority: 1,
                    check_app_code: sessionStorage.getItem('CheckSchoolAppCode'),
                    status: 1,
                    authonity: 1,
                }
            } else if (this.resourceType == 1) {
                params = {
                    page: this.page_index3,
                    size: this.page_size3,
                    resource_name: this.resourceKeyword,
                    data_type: 2,
                    status: 1,
                    authonity: 1,
                    check_app_code: sessionStorage.getItem('CheckSchoolAppCode')
                }
            } else if (this.resourceType == 0) {
                params = {
                    page: this.page_index3,
                    size: this.page_size3,
                    resource_name: this.resourceKeyword,
                    data_type: 2,
                    // status: 1,
                    // authonity: 0,
                    // create_id: this.$g.user_id,
                    check_app_code: sessionStorage.getItem('CheckSchoolAppCode'),
                    status: 1,
                    authonity: 1,
                }
            }

            this.$ajax.get('api/resource/resource', { params, func: { success: success } })
        },

        resourceSbulmit() {
            if (this.resourceSelection.length === 0) {
                this.$message.error('请勾选');
                return
            }


            for (let i = 0; i < this.resourceSelection.length; i++) {
                this.ruleForm.resourceArr.push({
                    name: this.resourceSelection[i].name,
                    id: this.resourceSelection[i].id,
                    origin: 2,
                })
            }
            /*去重 */
            let hash = {};
            let arr = this.ruleForm.resourceArr.reduceRight((item, next) => {
                hash[next.id] ? '' : hash[next.id] = true && item.push(next);
                return item
            }, [])

            this.ruleForm.resourceArr = arr

            this.resourceVisible = false;
        },
        removeResource(index){
            this.ruleForm.resourceArr.splice(index, 1)
             // this.$refs.uploaderRes.uploader.removeFile( this.$refs.uploaderRes.uploader.fileList[index])
            //  this.$refs.uploaderRes.uploader.fileList.splice(index, 1)
            // this.$refs.uploader.fileList.splice(index, 1)
            console.info(this.$refs.uploaderRes.uploader)
        },
        resourceChange(val) {
          this.page_index3 = val;
          this.getResourceList()
        },
        resourceSelectionChange(val) {
            this.resourceSelection = val;
        },
        fileAddRes(file) { /*本地文件上传 */
            if (file.size > 2000 * 1024 * 1024) {
                this.$message.error("附件大小不能超过2G");
                file.ignored = true;
                return false;
            }
            this.loading4 = true;
        },
        //上传成功的事件  
        fileSuccessRes(rootFile, file, message, chunk) {
            this.$refs.uploaderRes.uploader.cancel();
            this.loading4 = false;

            let mess = eval('(' + message + ')');
             mess = handleResponseData(mess);
            this.ruleForm.resourceArr.push({ 
                origin: 2, 
                name: mess.data.file_name, 
                file_path: mess.data.file_path ,
                post_fix: mess.data.post_fix,
                name_up:  mess.data.file_name
            })
        },
        fileFailRes(rootFile, file, message, chunk) {
            var mess = eval('(' + message + ')');
            if (parseInt(mess.error) === 401 && this.uploadRetryTimes > 0) {
                let callbcak = (data => {
                    this.uploadRetryTimes--;
                    this.$refs.uploaderRes.uploader.retry(file);
                })
                this.get_access_token(callbcak);
            } else {
                this.$message.error(mess.msg);
            }
        },
        resetForm(formName) {
            // this.$refs[formName].resetFields();
            this.$router.back(-1)// 
        },

        getTeacher() {

            let success = (res => {
                
                this.loading4 = false;
                this.teachList = res.data.data;
                this.total_sum4 = parseInt(res.data.data_sum);

                if (this.teachList.length > 0) {
                    for (let i = 0; i < this.teachList.length; i++) {
                       for (let j = 0; j < this.teachArr.length; j++) {
                           if (this.teachArr[j] == this.teachList[i].id) {
                               this.teachlistID.push(this.teachList[i])
                           }
                       }
                    }

                    /*去重 */
                    let hash = {};
                    let arr = this.teachlistID.reduceRight((item, next) => {
                        hash[next.id] ? '' : hash[next.id] = true && item.push(next);
                        return item
                    }, [])

                    this.teachlistID = arr
                    //获取原有老师的Id
                    for (let i = 0; i < this.teachlistID.length; i++) {
                        if (this.teachlistID[i].id) {
                           this.ruleForm.teachArr.push(this.teachlistID[i].id)
                        }
                    }
                }
            })

            this.$ajax({
                url: 'api/user/user',
                method: 'get',
                params: {
                    page: this.page_index4,
                    size: this.page_size4,
                    role_id: 6,
                    data_type: 2,
                    status: 1
                },
                func: { success: success }
            })
        },
        // 选择教师
        teachSbulmit() {
            if (this.teachlection.length === 0) {
                this.$message.error('请勾选');
                return
            }
           
           //原有的老师数组合并新添加的老师
            this.teachlistID.push.apply(this.teachlistID, this.teachlection)
            /*去重 */
            let hash = {};
            let arr = this.teachlistID.reduceRight((item, next) => {
                hash[next.id] ? '' : hash[next.id] = true && item.push(next);
                return item
            }, [])

            this.teachlistID = arr
            //老师的id
            this.teachlistID .forEach(item =>{
                this.ruleForm.teachArr.push(item.id)
            })

            this.teachVisible = false
        },

        //获取编辑数据
        getPrepare_Date(){
            let success = (res=>{
                this.ruleForm.type = res.data.type; 
                this.ruleForm.name = res.data.prepare_name
                this.ruleForm.target  = res.data.profile

                 this.$refs.selection.edit = {
                    section: res.data.study_section_id,
                    grade:  res.data.grade_id,
                    subject: res.data.subject_id
                }

                this.ruleForm.date  = res.data.end_time;
                //拼接出原有的老师
                 this.teachArr = res.data.teacher_id.split(',');

                this.getPrepar_fitle()
            });
            this.$ajax({
                url: 'api/prepare/prepare',
                method: 'get',
                params: {prepareid: this.$route.query.id},
                func: {success: success}
            })
        },
        //获取编辑-上传教案数据
        getPrepar_fitle(){
            let success = (res=>{
                let data = res.data;

                for (var i = 0; i < data.length; i++) {
                    if (data[i].res_type == '试题') {
                        this.ruleForm.testArr.push({
                            name: data[i].res_name,
                            id: data[i].res_id,
                            origin: data[i].origin,
                        })
                    }else if (data[i].res_type == '视频') {
                        this.ruleForm.videoArr.push({
                            name: data[i].res_name,
                            id: data[i].res_id,
                            origin: data[i].origin,
                        })
                    }else {
                        this.ruleForm.resourceArr.push({
                            name: data[i].res_name,
                            id: data[i].res_id,
                            origin: data[i].origin,
                        })
                    }
                }

            });

            this.$ajax({
                url: 'api/prepare/prepare_res_list',
                method: 'get',
                params: {prepareid: this.$route.query.id},
                func: {success: success}
            })
        },
        submitForm(formName) {
            
            //合并视频，试卷， 资源的数组
            let arr = this.ruleForm.videoArr
            let arr2 = arr.concat(this.ruleForm.testArr)
            let arr3 = arr2.concat(this.ruleForm.resourceArr)
            let arr4 = []
            for (let i = 0; i < arr3.length; i++) {
                console.log(arr3);
                arr4.push(`${arr3[i].id || arr3[i].file_path} , ${arr3[i].origin}, ${arr3[i].name_up === 'undefined' ? '' : arr3[i].name_up}`)
            }

            let teachArr = Array.from(new Set(this.ruleForm.teachArr));
            this.$refs[formName].validate((valid) => {

                if (valid) {
                    // 验证学段-年级-科目的值
                    this.$refs.selection.valid();
                    const flag = Object.values(this.$refs.selection.ruleForm).includes('');
                    if(flag)return;

                    let timestampToTime = deep_clone(this.ruleForm.date)
                    let success = (res=>{
                       this.$message.success(res.msg);

                       if (this.ruleForm.type === 0) {
                            this.$router.push({path: '/prepare'})
                       }else{
                            this.$router.push({path: '/prepare/collective'})
                       }
                    });

                  let params = {
                    type: this.ruleForm.type,
                    prepareid: this.$route.query.id,
                    preparename: this.ruleForm.name,
                    profile: this.ruleForm.target,

                    study_section_id: this.$refs.selection.ruleForm.section,
                    grade_id: this.$refs.selection.ruleForm.grade,
                    subject: this.$refs.selection.ruleForm.subject,

                    create_id: this.$g.user_id,
                    end_time:  this.ruleForm.date,
                    teacherid: teachArr,
                     videoid: arr4
                  }

                  this.$ajax.put('api/prepare/prepare', params, { func: { success: success } })

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
    }
}
</script>